package com.pkutech.carcasher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.content.ParkingLogAdapter;
import com.pkutech.carcasher.content.ParkingLogDataSource;
import com.pkutech.carcasher.model.ParkingLog;
import com.pkutech.carcasher.utils.CommonUtils;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import org.angmarch.views.NiceSpinner;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "历史停车查询"模块
 * Created by cyy on 2016/6/10.
 */
public class HistoryParkingActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.startTimeLabel)
    TextView startTimeLabel;
    @BindView(R.id.startTimeTxt)
    TextView startTimeTxt;
    TimePickerView startTime;
    @BindView(R.id.parkingSpinner)
    NiceSpinner parkingSpinner;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private MVCHelper<List<ParkingLog>> mvcHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_parking);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        //时间选择器
        startTime = new TimePickerView(this, TimePickerView.Type.ALL);
        //控制时间范围
//        Calendar calendar = Calendar.getInstance();
//        pvTime.setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR));
        startTime.setTime(new Date());
        startTime.setCyclic(false);
        startTime.setCancelable(true);
        //时间选择后回调
        startTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                startTimeTxt.setText(CommonUtils.getTime(date));
            }
        });
        //弹出时间选择器
        startTimeTxt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startTime.show();
            }
        });

        List<String> dataset = new LinkedList<String>(Arrays.asList("无锡停车场1", "无锡停车场2", "无锡停车场3", "无锡停车场4", "无锡停车场5"));
        parkingSpinner.attachDataSource(dataset);

        mvcHelper = new MVCSwipeRefreshHelper<List<ParkingLog>>(swipeRefreshLayout);
        // 设置数据源
        mvcHelper.setDataSource(new ParkingLogDataSource());
        // 设置适配器
        mvcHelper.setAdapter(new ParkingLogAdapter(this));

        // 加载数据
        mvcHelper.refresh();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.stop_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.stop_add) {
            Intent intent = new Intent(this, StopParkingActivity.class);
            startActivity(intent);
        }

        return super.onOptionsItemSelected(item);
    }
}
